package dyy.zc.sportruler.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import dyy.zc.sportruler.R;
import dyy.zc.sportruler.application.MyApplication;
import dyy.zc.sportruler.bean.StudentInfo;
import dyy.zc.sportruler.bean.UserInfo;
import dyy.zc.sportruler.listener.OnItemClickListener;
import dyy.zc.sportruler.views.CircleImageView;


/**
 * Created by chuan on 2017/3/28.
 */

public class TeacherAndStudentAdapter extends RecyclerView.Adapter<TeacherAndStudentAdapter.MyViewHolder> {
    private Context context;
    private OnItemClickListener onItemClickListener;
    private List<StudentInfo> list_usp;
    private UserInfo loginUser;

    public TeacherAndStudentAdapter(Context context, List<StudentInfo> list_usp) {
        this.context = context;
        this.list_usp = list_usp;
        loginUser = MyApplication.getInstance().readLoginUser();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setList_usp(List<StudentInfo> list_usp) {
        this.list_usp = list_usp;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(
                LayoutInflater.from(context).inflate(R.layout.student_item_recycleview, null), onItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.name.setText(list_usp.get(position).getName());
        holder.want.setText(list_usp.get(position).getWanted());
        holder.part.setText(list_usp.get(position).getPart());
        Glide.with(context).load(list_usp.get(position).getHead().getFileUrl())
                .error(R.mipmap.default_head)
                .centerCrop()
                .into(holder.head);
    }

    @Override
    public int getItemCount() {
        if (list_usp == null || list_usp.size() == 0) {
            return 0;
        }
        return list_usp.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnItemClickListener onItemClickListener;
        private CircleImageView head;
        private TextView name;
        private TextView want, part;

        public MyViewHolder(View itemView, OnItemClickListener onItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.onItemClickListener = onItemClickListener;
            head = (CircleImageView) itemView.findViewById(R.id.head);
            name = (TextView) itemView.findViewById(R.id.name);
            want = (TextView) itemView.findViewById(R.id.student_item_want);
            part = (TextView) itemView.findViewById(R.id.part);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
